package com.xianbeiying.app.ckgl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.adapter.EdUpTypeadapter;
import com.xianbeiying.app.ckgl.adapter.PlantCropAdapter;
import com.xianbeiying.app.ckgl.bean.CreatPlantBean;
import com.xianbeiying.app.ckgl.bean.GrowBean;
import com.xianbeiying.app.ckgl.bean.PlantCropBean;
import com.xianbeiying.app.ckgl.data.DataUtil;
import com.xianbeiying.app.ckgl.http.HttpCallBackUtil;
import com.xianbeiying.app.ckgl.model.GetModel;
import com.xianbeiying.app.ckgl.model.SetModel;
import com.xianbeiying.app.ckgl.util.ToastUtil;
import com.xianbeiying.app.ckgl.widght.NoScrollListView;
import com.xianbeiying.app.ckgl.widght.datetimepicker.date.DatePickerDialog;
import com.xianbeiying.app.ckgl.widght.popwindow.LayoutPopWindow;

import java.util.ArrayList;
import java.util.Calendar;

/**
 * 种植作物
 */
public class PlantCropActivity extends BaseActivity implements View.OnClickListener, DatePickerDialog.OnDateSetListener, HttpCallBackUtil {
    private ImageView iv_title_left;
    private TextView tv_title;
    private Button sc_btn;
    private ImageView iv_pc_ym, iv_pc_zz;
    private LinearLayout ly_pc_zw, ly_pc_time, ly_pc_cs;
    private TextView tv_pc_zw, tv_pc_time, tv_pc_cs;
    private SetModel setModel;
//    private TextView et_one_name, et_two_name, et_three_name, et_four_name;
//    private EditText et_one_sc, et_two_sc, et_three_sc, et_four_sc;
    private int CROPID, PLANTTYPE;
    private String PLANTNAME;
    private GetModel getModel;
    //添加
    private Button add_btn;
    //选择阶段
    private LinearLayout ly_pc_jd;
    private TextView tv_pc_jd;
    //阶段
    private TextView et_jieduan;
    //天数
    private EditText et_number;
    //列表
    private NoScrollListView listView;
    private PlantCropAdapter plantCropAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plant_crop);
        initView();
    }

    private Intent intent;

    private void initView() {
        intent = new Intent();
        setModel = new SetModel(this);
        setModel.addResponseListener(this);
        getModel = new GetModel(this);
        getModel.addResponseListener(this);
        iv_title_left = (ImageView) findViewById(R.id.iv_title_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("种植作物");
        iv_title_left.setOnClickListener(this);
        sc_btn = (Button) findViewById(R.id.sc_btn);
        iv_pc_ym = (ImageView) findViewById(R.id.iv_pc_ym);
        iv_pc_zz = (ImageView) findViewById(R.id.iv_pc_zz);
        sc_btn = (Button) findViewById(R.id.sc_btn);
        ly_pc_zw = (LinearLayout) findViewById(R.id.ly_pc_zw);
        ly_pc_time = (LinearLayout) findViewById(R.id.ly_pc_time);
//        ly_pc_cs = (LinearLayout) findViewById(R.id.ly_pc_cs);
        tv_pc_zw = (TextView) findViewById(R.id.tv_pc_zw);
        tv_pc_time = (TextView) findViewById(R.id.tv_pc_time);
//        tv_pc_cs = (TextView) findViewById(R.id.tv_pc_cs);
//        et_one_name = (TextView) findViewById(R.id.et_one_name);
//        et_one_sc = (EditText) findViewById(R.id.et_one_sc);
//        et_two_name = (TextView) findViewById(R.id.et_two_name);
//        et_two_sc = (EditText) findViewById(R.id.et_two_sc);
//        et_three_name = (TextView) findViewById(R.id.et_three_name);
//        et_three_sc = (EditText) findViewById(R.id.et_three_sc);
//        et_four_name = (TextView) findViewById(R.id.et_four_name);
//        et_four_sc = (EditText) findViewById(R.id.et_four_sc);
        add_btn = (Button) findViewById(R.id.add_btn);
        tv_pc_jd = (TextView) findViewById(R.id.tv_pc_jd);
        ly_pc_jd = (LinearLayout) findViewById(R.id.ly_pc_jd);
        et_jieduan = (TextView) findViewById(R.id.et_jieduan);
        et_number = (EditText) findViewById(R.id.et_number);
        listView = (NoScrollListView) findViewById(R.id.list_plant);
        sc_btn.setOnClickListener(this);
        ly_pc_jd.setOnClickListener(this);
        add_btn.setOnClickListener(this);
        iv_pc_ym.setOnClickListener(this);
        iv_pc_zz.setOnClickListener(this);
        ly_pc_zw.setOnClickListener(this);
        ly_pc_time.setOnClickListener(this);
//        ly_pc_cs.setOnClickListener(this);
        iv_pc_ym.setSelected(true);
    }

    private ArrayList<String> crop = new ArrayList<>();
    private ArrayList<Integer> number = new ArrayList<>();

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            //确定
            case R.id.sc_btn:
                //提交处理
                String plantType = tv_pc_zw.getText().toString().trim();
//                String plantTime = tv_pc_time.getText().toString().trim();
//                String pc_cs = tv_pc_cs.getText().toString().trim();
                if (plantType.isEmpty()) {
                    ToastUtil.showInfo(this, "请选择种植作物");
                    return;
                }
                if (crop.size()==0) {
                    ToastUtil.showInfo(this, "请选择种植阶段");
                    return;
                }
//                if(pc_cs.isEmpty()){
//                    ToastUtil.showInfo(this,"请选择种植成熟期");
//                    return;
//                }
//                setModel.ADDSYSTEM("",124,"",true);
                if(PLANTTYPE==0){
                    getData(growBean1);
                }else{
                    getData(growBean2);
                }
                break;
            //育苗
            case R.id.iv_pc_ym:
                showDefault();
                PLANTTYPE = 0;
                tv_pc_zw.setText("");
                tv_pc_jd.setText("");
                et_jieduan.setText("");
                et_number.setText("");
                et_number.setHint("天数");
                crop.clear();
                number.clear();
//                if(plantCropAdapter!=null){
//                    plantCropAdapter.notifyDataSetChanged();
//                }
                listView.setAdapter(null);
                iv_pc_ym.setSelected(true);
                break;
            //种植
            case R.id.iv_pc_zz:
                showDefault();
                PLANTTYPE = 1;
                tv_pc_zw.setText("");
                tv_pc_jd.setText("");
                et_jieduan.setText("");
                et_number.setText("");
                et_number.setHint("天数");
                crop.clear();
                number.clear();
                listView.setAdapter(null);
//                if(plantCropAdapter!=null){
//                    plantCropAdapter.notifyDataSetChanged();
//                }
                iv_pc_zz.setSelected(true);
                break;
            //种植作物
            case R.id.ly_pc_zw:
                intent.setClass(PlantCropActivity.this, ChoicePlantActivity.class);
                startActivityForResult(intent, 10);
                break;
            //添加
            case R.id.add_btn:
                 String growplant=et_jieduan.getText().toString().trim();
                 String numer=et_number.getText().toString().trim();
                 if(growplant.isEmpty()){
                     ToastUtil.showInfo(PlantCropActivity.this,"请选择阶段");
                     return;
                 }
                if(numer.isEmpty()){
                    ToastUtil.showInfo(PlantCropActivity.this,"请输入天数");
                    return;
                }
                if(DataUtil.isStringRest(crop,growplant)){
                    ToastUtil.showInfo(PlantCropActivity.this,"您已添加该阶段");
                    return;
                }
                crop.add(growplant);
                number.add(Integer.valueOf(numer));
                plantCropAdapter=new PlantCropAdapter(PlantCropActivity.this,crop,number);
                listView.setAdapter(plantCropAdapter);
                et_jieduan.setText("");
                et_number.setText("");
                tv_pc_jd.setText("");
                et_number.setHint("天数");
                break;
            //选择生长阶段
            case R.id.ly_pc_jd:
                if(PLANTTYPE==0){
                    if(growBean1!=null){
                        setPopPlantType(growBean1);
                    }else{
                        getModel.GET_CROP_STEP_INFO("GET_CROP_STEP_INFO", 0, true);
                    }
                }else{
                    if(growBean2!=null){
                        setPopPlantType(growBean2);
                    }else{
                        getModel.GET_CROP_STEP_INFO("GET_CROP_STEP_INFO", 1, true);
                    }
                }
                break;
            //种植时间
            case R.id.ly_pc_time:
                ly_pc_time.setTag("1");
                getTime();
                break;
//            //预计成熟期
//            case R.id.ly_pc_cs:
//                ly_pc_cs.setTag("2");
//                getTime();
//                break;
        }
    }

    private void getTime() {
        final Calendar calendar = Calendar.getInstance();
        final DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(this, calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), true);
        datePickerDialog.setYearRange(1985, 2028);
        datePickerDialog.setCloseOnSingleTapDay(true);
        datePickerDialog.show(getSupportFragmentManager(), "");
    }

    private void showDefault() {
        iv_pc_ym.setSelected(false);
        iv_pc_zz.setSelected(false);
    }

    /**
     * 大棚类型
     */
    private String[] dpType;
    private LayoutPopWindow planDptype;
    private ListView planDptypeList;
    private EdUpTypeadapter cpDpAdapter;

    private void popWindowPlanDptype() {
        planDptype = new LayoutPopWindow(this, R.layout.pop_window_edupgh, tv_pc_jd);
        planDptypeList = planDptype.getView(R.id.pop_edup_lists);
        if (dpType == null && dpType.length == 0) {
            return;
        }
        cpDpAdapter = new EdUpTypeadapter(this, dpType);
        planDptypeList.setAdapter(cpDpAdapter);
        planDptypeList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                planDptype.dismiss();
                tv_pc_jd.setText(dpType[position]);
                et_jieduan.setText(dpType[position]);
            }
        });
    }

    @Override
    public void onDateSet(DatePickerDialog datePickerDialog, int year, int month, int day) {
        if (ly_pc_time.getTag() != null && ly_pc_time.getTag().equals("1")) {
            tv_pc_time.setText(year + "-" + (month + 1) + "-" + day);
        }
        if (ly_pc_cs.getTag() != null && ly_pc_cs.getTag().equals("2")) {
            tv_pc_cs.setText(year + "-" + (month + 1) + "-" + day);
        }
    }
   private ArrayList<String> choiceId;
   private ArrayList<String> plantName;
   private ArrayList<String> cropTypeId;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10){
            if (resultCode == 20){
                tv_pc_zw.setText(data.getStringExtra("zw"));
                PLANTNAME = tv_pc_zw.getText().toString().trim();
                choiceId=data.getStringArrayListExtra("choiceId");
                plantName=data.getStringArrayListExtra("zwlist");
                cropTypeId=data.getStringArrayListExtra("cropTypeId");
                CROPID = data.getIntExtra("zwId", 0);
            }
        }

    }

    private PlantCropBean plantCropBean;
    private GrowBean growBean1,growBean2;

    @Override
    public void onCallBackData(String url, Object content) {
        if (url.equals("创建大棚种植信息")) {
            if (content != null) {
                plantCropBean = JSON.parseObject(new String((byte[]) content), PlantCropBean.class);
                if (plantCropBean.isR()) {
                    ToastUtil.showInfo(PlantCropActivity.this, "添加成功");
                    finish();
                } else {
                    ToastUtil.showInfo(PlantCropActivity.this, plantCropBean.getERR_MSG());
                }

            }
        }

        if (url.equals("作物生长阶段信息")){
            GrowBean   growBean = JSON.parseObject(new String((byte[]) content), GrowBean.class);
            if (growBean.isR()) {
                if (growBean.getV().getCROPSTEPINFOS() != null && growBean.getV().getCROPSTEPINFOS().size() != 0) {
//                et_one_name.setText(growBean.getV().getCROPSTEPINFOS().get(0).getStepName());
//                et_two_name.setText(growBean.getV().getCROPSTEPINFOS().get(1).getStepName());
//                et_three_name.setText(growBean.getV().getCROPSTEPINFOS().get(2).getStepName());
//                et_four_name.setText(growBean.getV().getCROPSTEPINFOS().get(3).getStepName());}
                    if(PLANTTYPE==0){
                     growBean1=growBean;
                    }else{
                     growBean2=growBean;
                    }
                    setPopPlantType(growBean);
                }
            }
        }
    }

    private void setPopPlantType( GrowBean   growBean){
        int size = growBean.getV().getCROPSTEPINFOS().size();
        dpType = new String[size];
        for (int i = 0; i < size; i++) {
            dpType[i] = growBean.getV().getCROPSTEPINFOS().get(i).getStepName();
        }
        popWindowPlanDptype();

    }
    private ArrayList<CreatPlantBean> arrayList = new ArrayList<>();

    public void getData(GrowBean growBean) {
//        int oneSc = Integer.valueOf(et_one_sc.getText().toString().trim());
//        int twoSc = Integer.valueOf(et_two_sc.getText().toString().trim());
//        int thressSc = Integer.valueOf(et_three_sc.getText().toString().trim());
//        int fourSc = Integer.valueOf(et_four_sc.getText().toString().trim());
        arrayList.clear();
        if (growBean.getV().getCROPSTEPINFOS() != null && growBean.getV().getCROPSTEPINFOS().size() != 0) {
//            CreatPlantBean c1 = new CreatPlantBean(1 + "", growBean.getV().getCROPSTEPINFOS().get(0).getStepName(), growBean.getV().getCROPSTEPINFOS().get(0).getStepColor(), oneSc + "");
//            CreatPlantBean c2 = new CreatPlantBean(2 + "", growBean.getV().getCROPSTEPINFOS().get(1).getStepName(), growBean.getV().getCROPSTEPINFOS().get(1).getStepColor(), twoSc + "");
//            CreatPlantBean c3 = new CreatPlantBean(3 + "", growBean.getV().getCROPSTEPINFOS().get(2).getStepName(), growBean.getV().getCROPSTEPINFOS().get(2).getStepColor(), thressSc + "");
//            CreatPlantBean c4 = new CreatPlantBean(4 + "", growBean.getV().getCROPSTEPINFOS().get(3).getStepName(), growBean.getV().getCROPSTEPINFOS().get(3).getStepColor(), fourSc + "");
//            arrayList.add(c1);
//            arrayList.add(c2);
//            arrayList.add(c3);
//            arrayList.add(c4);
             int size=crop.size();
             for(int i=0;i<size;i++){
                 CreatPlantBean creatPlantBean=new CreatPlantBean((i+1)+"",growBean.getV().getCROPSTEPINFOS().get(i).getStepName(),
                         growBean.getV().getCROPSTEPINFOS().get(i).getStepColor(),number.get(i)+"");
                 arrayList.add(creatPlantBean);
             }
//            setModel.CREATE_CROPSTEP("CREATE_CROPSTEP", CROPID + "", PLANTTYPE + "", PLANTNAME, arrayList, true);
            setModel.CREATE_CROPSTEPss("CREATE_CROPSTEP", choiceId, cropTypeId.get(0),PLANTTYPE+"", plantName, arrayList, true);

        }
    }
}
